package com.ruoyi.web.controller.module.union;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.MyUtils;
import com.ruoyi.market.domain.*;
import com.ruoyi.market.service.*;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 联合数据 字典信息 信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/union/unionDict")
public class UnionDictController extends BaseController
{
    @Autowired
    private IProjectService projectService;

    @Autowired
    private IUnitService unitService;

    @Autowired
    private ILevelService levelService;

    @Autowired
    private IPriceService priceService;

    @Autowired
    private IGiftService giftService;

    @Autowired
    private IConfigBaseService configBaseService;

    @Autowired
    private IConfigOrderService configOrderService;

    @Autowired
    private IConfigGlobalService configGlobalService;

    @Autowired
    private ISysDictDataService dictDataService;

    @GetMapping("/getUnionDictList")
    public AjaxResult getDictList(HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if (shopId == null || shopId.equals("")) {
            return error("店铺基本配置失败，参数错误");
        }
        AjaxResult ajax = AjaxResult.success();
        PageHelper.startPage(1, 10000).setReasonable(true);

        Project project = new Project();
        project.setShopId(shopId);
        project.setStatus(UserConstants.NORMAL);
        PageHelper.orderBy("project_sort asc");
        List<Project> projectList = projectService.selectProjectList(project);
        ajax.put("projectList", projectList);
        PageHelper.clearPage();

        Unit unit = new Unit();
        unit.setShopId(shopId);
        PageHelper.orderBy("unit_sort asc");
        List<Unit> unitList = unitService.selectUnitList(unit);
        ajax.put("unitList", unitList);
        PageHelper.clearPage();

        Level level = new Level();
        level.setShopId(shopId);
        PageHelper.orderBy("level_sort asc");
        List<Level> levelList = levelService.selectLevelList(level);
        ajax.put("levelList", levelList);
        PageHelper.clearPage();

        Gift gift = new Gift();
        gift.setShopId(shopId);
        List<Gift> giftList = giftService.selectGiftList(gift);
        ajax.put("giftList", giftList);
        PageHelper.clearPage();

        Price price = new Price();
        price.setShopId(shopId);
        price.setStatus(UserConstants.NORMAL);
        PageHelper.orderBy("price_sort asc");
        List<Price> priceList = priceService.selectPriceList(price);

        ConfigOrder sysConfigOrder = configOrderService.selectConfigOrderByShopId(shopId);
        if(sysConfigOrder.getStatusNightOrder().equals(UserConstants.YES)){
            if(sysConfigOrder.getNightOrderStart() != null &&
                    sysConfigOrder.getNightOrderEnd() != null &&
                    MyUtils.isTimeBetween(sysConfigOrder.getNightOrderStart(), sysConfigOrder.getNightOrderEnd())){
                for(Price p : priceList){
                    p.setAmount(p.getAmount().multiply(BigDecimal.valueOf(sysConfigOrder.getNightOrderRate())));
                }
            }
        }
        if(sysConfigOrder.getStatusFestivalOrder().equals(UserConstants.YES)){
            SysDictData dictData = new SysDictData();
            dictData.setDictType("app_festival_list");
            List<SysDictData> dictList = dictDataService.selectDictDataList(dictData);
            for(SysDictData d : dictList){
                String[] strArray = d.getDictData().split("-");
                if(MyUtils.isWithinDateRange(strArray[0], strArray[1])){
                    for(Price p : priceList){
                        p.setAmount(p.getAmount().multiply(BigDecimal.valueOf(sysConfigOrder.getFestivalOrderRate())));
                    }
                }
            }
        }
        ajax.put("priceList", priceList);
        PageHelper.clearPage();

        return ajax;
    }

    @GetMapping("/getUnionConfig")
    public AjaxResult getUnionConfig(HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if (shopId == null || shopId.equals("")) {
            return error("店铺基本配置失败，参数错误");
        }
        AjaxResult ajax = AjaxResult.success();
        ConfigBase sysConfigBase = configBaseService.selectConfigBaseByShopId(shopId);
        ajax.put("sysConfigBase", sysConfigBase);

        ConfigOrder sysConfigOrder = configOrderService.selectConfigOrderByShopId(shopId);
        ajax.put("sysConfigOrder", sysConfigOrder);

        ConfigGlobal sysConfigGlobal = configGlobalService.selectConfigGlobalByShopId(shopId);
        ajax.put("sysConfigGlobal", sysConfigGlobal);

        return ajax;
    }
}
